


//
//  DoorSystemTabBarController.m
//  RONGAO
//
//  Created by ZhangYeKun on 2017/2/24.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorSystemTabBarController.h"
#import "DoorSystemCheckViewController.h"
#import "DoorSystemHomeViewController.h"
#import "DoorSystemSettingViewController.h"
#import "BaseNavigationController.h"

@interface DoorSystemTabBarController ()
@property (strong, nonatomic) NSMutableArray *btnList;

@end

@implementation DoorSystemTabBarController


- (void)leftBack{

    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    [self setupTabBarBtn];
    [self setSelectedIndex:1];
    UIButton *btn = self.btnList[1];
    [self buttonAction:btn];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leftBack) name:@"backVC" object:nil];
}

- (void)setupTabBarBtn{
    
    
    /**
     工程
     */
    DoorSystemCheckViewController *firstVC = [[DoorSystemCheckViewController alloc]init];
    UINavigationController *naFirst = [[BaseNavigationController alloc]initWithRootViewController:firstVC];
    naFirst.toolbarHidden = YES;
    
    /**
     home
     */
 //  DoorSystemHomePlayViewController *secondVC = [[DoorSystemHomePlayViewController alloc]init];
    
    DoorSystemHomeViewController *secondVC = [[DoorSystemHomeViewController alloc] initWithNibName:@"DoorSystemHomeViewController" bundle:nil];
    
 
    DoorSystemSettingViewController *thirdVC = [[DoorSystemSettingViewController alloc]init];
    UINavigationController *naThird = [[BaseNavigationController alloc]initWithRootViewController:thirdVC];
    naThird.toolbarHidden = YES;

    [self setViewControllers:@[naFirst,secondVC,naThird]];
    
    NSArray *norImageArray = @[@"icon_see_nor",@"icon_home_nor",@"icon_set_nor"];
    NSArray *selImageArray = @[@"icon_see_sel",@"icon_home_sel",@"icon_set_sel"];
    NSArray *titleArray = @[@"查看",@"主页",@"设置"];
    
    UIView *tabBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];
    tabBar.backgroundColor = [UIColor colorWithHexString:@"#231815"];
    
    for (int i = 0; i < norImageArray.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH/norImageArray.count, 0, SCREEN_WIDTH/norImageArray.count, TabBarHeight)];
        [button setImage:[UIImage imageNamed:selImageArray[i]] forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:norImageArray[i]] forState:UIControlStateNormal];
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:90/255.0 green:90/255.0 blue:90/255.0 alpha:1] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:139/255.0 green:115/255.0 blue:79/255.0 alpha:1] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"button_selected-"] forState:UIControlStateSelected];
        //        [button setBackgroundImage:[UIImage imageNamed:@"tab_bg"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12.f];
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:3.f];
        [tabBar addSubview:button];
        
        button.tag = 10 + i;
        button.selected = (0 == i)?YES:NO;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnList addObject:button];
    }
    
    [self hideBottomHairline];
    [self.tabBar addSubview:tabBar];
    
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
   
}
- (BOOL)shouldAutorotate{
    return YES;
}
- (NSMutableArray *)btnList
{
    if (!_btnList) {
        _btnList = [[NSMutableArray alloc]init];
    }
    return _btnList;
}

- (void)hideBottomHairline {
    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self.tabBar];
    navBarHairlineImageView.hidden = YES;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)buttonAction:(UIButton *)sender
{
    self.selectedIndex = sender.tag - 10;
    for (UIButton *btn in self.btnList) {
        btn.selected = NO;
    }
    sender.selected = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
